﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace handyTest.Common
{
    class Temperature : Control
    {

        static double maxValue, minValue, interval, curValue;

        static Geometry curGeometry;

        static Temperature()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Temperature), new FrameworkPropertyMetadata(typeof(Temperature)));
        }

        /// <summary>
        /// 最大值
        /// </summary>
        public double MaxValue
        {
            get { return (double)GetValue(MaxValueProperty); }
            set
            {
                //SetValue(MaxValueProperty, value);
                maxValue = value;
            }
        }
        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(double), typeof(Temperature), new UIPropertyMetadata(40.0));


        /// <summary>
        /// 最小值
        /// </summary>
        public double MinValue
        {
            get { return (double)GetValue(MinValueProperty); }
            set
            {
                // SetValue(MinValueProperty, value);
                minValue = value;
            }
        }
        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(double), typeof(Temperature), new UIPropertyMetadata(-10.0));

        /// <summary>
        /// 步长
        /// </summary>
        public double Interval
        {
            get { return (double)GetValue(IntervalProperty); }
            set
            {
                //SetValue(IntervalProperty, value); 
                interval = value;
            }
        }
        public static readonly DependencyProperty IntervalProperty =
            DependencyProperty.Register("Interval", typeof(double), typeof(Temperature), new UIPropertyMetadata(10.0));

        /// <summary>
        /// 最新值
        /// </summary>
        public double CurValue
        {
            get
            {
                return (double)GetValue(CurValueProperty);
            }
            set
            {
                SetValue(CurValueProperty, value);
                curValue = value;
                PaintPath();
            }
        }
        public static readonly DependencyProperty CurValueProperty =
      DependencyProperty.Register("CurValue", typeof(double), typeof(Temperature), new UIPropertyMetadata(OnCurrentValueChanged));



        ///// <summary>
        ///// 最新值plus
        ///// </summary>
        //public double CurrentValue
        //{
        //    get { return (double)GetValue(CurrentValueProperty); }
        //    set
        //    {
        //        SetValue(CurValueProperty, value);
        //        curValue = value;
        //        PaintPath();
        //    }
        //}
        //public static readonly DependencyProperty CurrentValueProperty =
        //    DependencyProperty.Register("CurrentValue", typeof(double), typeof(Temperature), new UIPropertyMetadata(OnCurrentValueChanged));


        /// <summary>
        /// 当前值的图形坐标点
        /// </summary>
        public Geometry CurrentGeometry
        {
            get { return (Geometry)GetValue(CurrentGeometryProperty); }

            set { SetValue(CurrentGeometryProperty, value); curGeometry = value; }
        }
        public static readonly DependencyProperty CurrentGeometryProperty =
            DependencyProperty.Register("CurrentGeometry", typeof(Geometry), typeof(Temperature), new PropertyMetadata(Geometry.Parse(@"M 2 132.8
                              a 4 4 0 0 1 4 -4
                              h 18
                              a 4 4 0 0 1 4 4
                              v 32.2
                              a 4 4 0 0 1 -4 4
                              h -18
                              a 4 4 0 0 1 -4 -4 z")));

        /// <summary>
        /// 获取最新值改变响应
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnCurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Temperature temperature = new();
            var propName = e.Property.Name.Trim();
            switch (propName)
            {
                case "curValue":
                    temperature.CurValue = Convert.ToDouble(e.NewValue);
                    break;
                case "MaxValue":
                    temperature.MaxValue = Convert.ToDouble(e.NewValue);
                    break;
                case "MinValue":
                    temperature.MinValue = Convert.ToDouble(e.NewValue);
                    break;
                case "Interval":
                    temperature.Interval = Convert.ToDouble(e.NewValue);
                    break;
                default:
                    break;
            }

        }



        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PaintPath();
        }

        [Obsolete]
        protected override void OnRender(DrawingContext drawingContext)
        {
            SolidColorBrush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#82848A"));
            Rect rect = new Rect();
            rect.Width = 30;
            rect.Height = 169;
            drawingContext.DrawRoundedRectangle(Brushes.Transparent,
                new Pen(brush, 2d),
                rect, 8d, 8d);

            #region 华氏温度

            //drawingContext.DrawText(GetFormattedText("华"), new Point(-49, 115));


            //drawingContext.DrawText(GetFormattedText("氏"), new Point(-49, 115 + 14));


            //drawingContext.DrawText(GetFormattedText("温"), new Point(-49, 115 + 28));


            //drawingContext.DrawText(GetFormattedText("度"), new Point(-49, 115 + 42));

            #endregion

            #region 摄氏温度


            //drawingContext.DrawText(GetFormattedText("摄", FlowDirection.LeftToRight), new Point(75, 115));

            //drawingContext.DrawText(GetFormattedText("氏", FlowDirection.LeftToRight), new Point(75, 115 + 14));


            //drawingContext.DrawText(GetFormattedText("温", FlowDirection.LeftToRight), new Point(75, 115 + 28));


            //drawingContext.DrawText(GetFormattedText("度", FlowDirection.LeftToRight), new Point(75, 115 + 42));

            #endregion

            #region 画刻度

            var total_Value = MaxValue - MinValue;

            var cnt = total_Value / Interval;

            var one_value = 161d / cnt;

            for (int i = 0; i <= cnt; i++)
            {
                var formattedText = GetFormattedText($"{MaxValue - (i * Interval)}", FlowDirection.LeftToRight);

                drawingContext.DrawText(formattedText, new Point(43, i * one_value - (formattedText.Height / 2d)));//减去字体高度的一半

                formattedText = GetFormattedText($"{(MaxValue - (i * Interval)) * 1.8d + 32d}");

                drawingContext.DrawText(formattedText, new Point(-13, i * one_value - (formattedText.Height / 2d)));

                if (i != 0 && i != 5)
                {
                    drawingContext.DrawLine(new Pen(Brushes.Black, 1d),
                        new Point(4, i * one_value), new Point(6, i * one_value));

                    drawingContext.DrawLine(new Pen(Brushes.Black, 1d),
                        new Point(24, i * one_value), new Point(26, i * one_value));
                }
            }

            #endregion
        }

        [Obsolete]
        private FormattedText GetFormattedText(string text, FlowDirection flowDirection = FlowDirection.RightToLeft)
        {
            return new FormattedText(text,
               CultureInfo.CurrentUICulture,
               flowDirection,
               new Typeface("Microsoft YaHei"),
               14d,
               new SolidColorBrush((Color)ColorConverter.ConvertFromString("#82848A")));
        }



        /// <summary>
        /// 数值改变
        /// </summary>
        public static void PaintPath()
        {
            var one_value = 161d / ((maxValue - minValue) / interval);

            var width = 26d;

            var height = 169d - (maxValue - curValue) * (one_value / interval);

            var x = 2d;

            var y = 169d - (169d - (maxValue - curValue) * (one_value / interval));


            curGeometry = Geometry.Parse($@"M 2 {y + 4}
                              a 4 4 0 0 1 4 -4
                              h {width - 8}
                              a 4 4 0 0 1 4 4
                              v {height - 8}
                              a 4 4 0 0 1 -4 4
                              h -{width - 8}
                              a 4 4 0 0 1 -4 -4 z");
        }
    }
}
